package com.qfedu.provider.controller;

import com.qfedu.provider.service.DbDynamicImagesService;
import com.qfedu.provider.util.MyMinioUtil;
import com.qfedu.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import com.qfedu.provider.entity.DbDynamicImages;
import com.qfedu.provider.service.DbDynamicImagesService;
import com.qfedu.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @date 2024/11/23 下午12:23
 */
@RestController
@RequestMapping("server/images")
@Tag(name = "动态图片管理")
@RequiredArgsConstructor
public class DynamicImages {
    private final RocketMQTemplate rocketMQTemplate;
    private final DbDynamicImagesService dbDynamicImagesService;
//    private final DbDynamicImagesService service;

    @PostMapping("uploadOne")
    public String upload() {
        return "上传成功";
    }

//            rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_MINIO_PICTURE,);

    //    @PostMapping("uploadMore")
//    @Operation(summary = "批量上传图片")
//    public R uploadMore(@RequestPart("files") MultipartFile[] files) throws IOException {
//        ArrayList<String> fileUrlList = new ArrayList<>();
//        for (MultipartFile file : files) {
//            String originalFilename = file.getOriginalFilename();
//            String newFileName = System.currentTimeMillis() + "_" + originalFilename;
//            byte[] data = file.getBytes();
//            String url = MyMinioUtil.upload(newFileName, data);
////            String url = dbDynamicImagesService.upload(newFileName, data);
//            fileUrlList.add(url);
////            rocketMQTemplate.convertAndSend(RocketmqConfig.TOPIC_MINIO_PICTURE,url);
//        }
//        System.out.println("1");
//        return R.ok(fileUrlList);
//    }
    @PostMapping(value = "/upload")
    @Operation(summary = "上传文件、对象、资源")
    public String upload(@RequestPart MultipartFile file,Integer dynamicsId) throws IOException {
        String newFileName = System.currentTimeMillis() + "_" + file.getOriginalFilename();
        byte[] data = file.getBytes();
        System.out.println("转换成byte数组后"+data);
        String url = MyMinioUtil.upload(newFileName, data);
        System.out.println("util后");
        DbDynamicImages dbDynamicImages = new DbDynamicImages(dynamicsId, url);
        boolean save = dbDynamicImagesService.save(dbDynamicImages);
        if (save){
            return url;
        }else {
            return "上传失败";
        }
    }
}